<template>
  <div class="dialog">
    <el-dialog
      :title="dialog.title"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :modal-append-to-body="false"
      :visible.sync="dialog.show"
    >
      <el-form ref="MailForm" :model="MailForm" label-width="150px">
        <el-form-item label="邮件描述：">
          <el-input
            :disabled="dialog.mailSearch"
            v-model="MailForm.describe"
            placeholder="请输入邮件描述"
          ></el-input>
        </el-form-item>
        <span
          style="
            display: block;
            font-size: 15px;
            text-align: center;
            padding: 10px 0;
            margin-top: -12px;
            color: gray;
          "
        >
          --------------------邮件条件--------------------
        </span>
        <el-form-item label="邮件生效时间:">
          <el-date-picker
            :disabled="dialog.mailSearch"
            v-model="MailForm.effectStartTime"
            type="datetime"
            value-format="timestamp"
            placeholder="请选择邮件生效时间"
          ></el-date-picker>
          <span style="margin-left: 15px">邮件失效时间：</span>
          <el-date-picker
            :disabled="dialog.mailSearch"
            v-model="MailForm.effectEndTime"
            type="datetime"
            value-format="timestamp"
            placeholder="请选择邮件失效时间"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="玩家注册起始时间:">
          <el-date-picker
            :disabled="dialog.mailSearch"
            v-model="MailForm.userRegStartTime"
            type="datetime"
            value-format="timestamp"
            placeholder="选择玩家注册起始时间"
          ></el-date-picker>
          <span style="margin-left: 15px">结束时间：</span>
          <el-date-picker
            :disabled="dialog.mailSearch"
            v-model="MailForm.userRegEndTime"
            type="datetime"
            value-format="timestamp"
            placeholder="选择玩家注册结束时间"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="服务器id:">
          <el-input
            :disabled="dialog.mailSearch"
            v-model="MailForm.server_ids"
            placeholder="请输入服务器id"
          ></el-input>
        </el-form-item>

        <el-form-item label="发送指定玩家(UID):">
          <el-input
            :disabled="dialog.mailSearch"
            v-model="MailForm.uids"
            placeholder="请输入发送指定玩家(UID)"
          ></el-input>
        </el-form-item>

        <el-form-item label="玩家最小等级:">
          <el-input
            :disabled="dialog.mailSearch"
            v-model.number="MailForm.userMinLevel"
            placeholder="请输入玩家最小等级"
          ></el-input>
        </el-form-item>

        <el-form-item label="玩家最低主线关卡ID:">
          <el-input
            :disabled="dialog.mailSearch"
            v-model.number="MailForm.userMinAfk"
            placeholder="请输入玩家最低主线关卡ID"
          ></el-input>
        </el-form-item>
        <span
          style="
            display: block;
            font-size: 15px;
            text-align: center;
            padding: 10px 0;
            margin-top: -12px;
            color: gray;
          "
        >
          --------------------邮件内容--------------------
        </span>
        <el-form-item label="中文标题:">
          <el-input
            :disabled="dialog.mailSearch"
            v-model="MailForm.cnTitle"
            placeholder="请输入中文标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="中文内容:">
          <el-input
            :disabled="dialog.mailSearch"
            :autosize="{ minRows: 6 }"
            type="textarea"
            v-model="MailForm.cnContent"
            placeholder="请输入中文内容"
          ></el-input>
        </el-form-item>

        <el-form-item label="英文标题:">
          <el-input
            :disabled="dialog.mailSearch"
            v-model="MailForm.enTitle"
            placeholder="请输入英文标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="英文内容:">
          <el-input
            :disabled="dialog.mailSearch"
            :autosize="{ minRows: 6 }"
            type="textarea"
            v-model="MailForm.enContent"
            placeholder="请输入英文内容"
          ></el-input>
        </el-form-item>

        <el-form-item label="法文标题:">
          <el-input
            :disabled="dialog.mailSearch"
            v-model="MailForm.frenchTitle"
            placeholder="请输入法文标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="法文内容:">
          <el-input
            :disabled="dialog.mailSearch"
            :autosize="{ minRows: 6 }"
            type="textarea"
            v-model="MailForm.frenchContent"
            placeholder="请输入法文内容"
          ></el-input>
        </el-form-item>

        <el-form-item label="德文标题:">
          <el-input
            :disabled="dialog.mailSearch"
            v-model="MailForm.germanTitle"
            placeholder="请输入德文标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="德文内容:">
          <el-input
            :disabled="dialog.mailSearch"
            :autosize="{ minRows: 6 }"
            type="textarea"
            v-model="MailForm.germanContent"
            placeholder="请输入德文内容"
          ></el-input>
        </el-form-item>

        <el-form-item label="俄文标题:">
          <el-input
            :disabled="dialog.mailSearch"
            v-model="MailForm.russianTitle"
            placeholder="请输入俄文标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="俄文内容:">
          <el-input
            :disabled="dialog.mailSearch"
            :autosize="{ minRows: 6 }"
            type="textarea"
            v-model="MailForm.russianContent"
            placeholder="请输入俄文内容"
          ></el-input>
        </el-form-item>

        <div v-for="(item, index) in MailForm.award" :key="index">
          <template>
            <el-form-item label="物品id：">
              <el-input
                :disabled="dialog.mailSearch"
                style="width: 35%"
                placeholder="物品id"
                v-model.number="item.goodsId"
              ></el-input>
              <span style="margin-left: 20px; padding: 10px; font-size: 15px">
                共
              </span>
              <el-input
                :disabled="dialog.mailSearch"
                style="width: 35%"
                v-model.number="item.goodsNum"
                controls-position="right"
              ></el-input>
              <span style="padding: 10px; font-size: 15px">
                个
              </span>
               <el-button 
                 class="el-icon-delete" 
                 :disabled="dialog.mailSearch" 
                 style="color:red;" 
                 @click="deleteItem(index)"
                ></el-button>
            </el-form-item>
          </template>
        </div>
        <el-button
          :disabled="dialog.mailSearch"
          type="primary"
          class="btn"
          @click="add"
          >添加物品id和数量</el-button
        >
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleclose()">取 消</el-button>
        <el-button
          type="primary"
          :disabled="dialog.mailSearch"
          @click="handleAddEdit()"
        >确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "MailDialog",
  // 获取父亲传过来的值
  props: {
    dialog: Object,
    MailForm: Object,
  },
  data() {
    return {
      mailshow: false,
    };
  },
  methods: {
    add() {
      this.MailForm.award.push({goodsId: "",goodsNum: ""});
    },
    deleteItem(index){
       this.MailForm.award.splice(index, 1)
    },
    handleclose() {
      this.dialog.show = false;
    },
    onSelected(val) {
      if (val == 0) {
        this.mailshow = true;
      } else {
        this.mailshow = false;
      }
    },
    handleAddEdit() {
      var awardArr=[]
      this.MailForm.award.forEach(item=>{
        if(item.goodsId===""&&item.goodsNum===""){
          return;
        }else{
          awardArr.push(item)
        }
      })
      if(this.MailForm.describe===""||this.MailForm.describe==undefined){
        this.$message({type:"warning",message:"邮件描述不能为空"});
        return;
      }else if(this.MailForm.cnTitle===""||this.MailForm.cnTitle==undefined){
        this.$message({type:"warning",message:"请输入中文标题"});
        return;
      }else if(this.MailForm.cnContent===""||this.MailForm.cnContent==undefined){
        this.$message({type:"warning",message:"请输入中文内容"});
        return;
      }else if(this.MailForm.enTitle===""||this.MailForm.enTitle==undefined){
        this.$message({type:"warning",message:"请输入英文标题"});
        return;
      }else if(this.MailForm.enContent===""||this.MailForm.enContent==undefined){
        this.$message({type:"warning",message:"请输入英文内容"});
        return;
      }else if(this.MailForm.frenchTitle===""||this.MailForm.frenchTitle==undefined){
        this.$message({type:"warning",message:"请输入法文标题"});
        return;
      }else if(this.MailForm.frenchContent===""||this.MailForm.frenchContent==undefined){
        this.$message({type:"warning",message:"请输入法文内容"});
        return;
      }else if(this.MailForm.germanTitle===""||this.MailForm.germanTitle==undefined){
        this.$message({type:"warning",message:"请输入德文标题"});
        return;
      }else if(this.MailForm.germanContent===""||this.MailForm.germanContent==undefined){
        this.$message({type:"warning",message:"请输入德文内容"});
        return;
      }else if(this.MailForm.russianTitle===""||this.MailForm.russianTitle==undefined){
        this.$message({type:"warning",message:"请输入俄文标题"});
        return;
      }else if(this.MailForm.russianContent===""||this.MailForm.russianContent==undefined){
        this.$message({type:"warning",message:"请输入俄文内容"});
        return;
      }else{
          var uidsarr = this.MailForm.uids.split(",");
          let specifiedArr = [];
          for (var k = 0; k < uidsarr.length; k++) {
            if (uidsarr[k] == 0) {
              uidsarr[k] = "";
            } else {
              specifiedArr.push(Number(uidsarr[k]));
            }
          }
          var server_idsarr = this.MailForm.server_ids.split(",");
          let specifiedServerIds = [];
          for (var k = 0; k < server_idsarr.length; k++) {
            if (server_idsarr[k] == 0) {
              server_idsarr[k] = "";
            } else {
              specifiedServerIds.push(Number(server_idsarr[k]));
            }
          }
          let obj = {
            describe: this.MailForm.describe,
            mailId: this.MailForm.mailId,
            effectStartTime: this.MailForm.effectStartTime,
            effectEndTime: this.MailForm.effectEndTime,
            userRegStartTime: this.MailForm.userRegStartTime,
            userRegEndTime: this.MailForm.userRegEndTime,
            award: awardArr,
            specifiedUid: specifiedArr,
            cnTitle: this.MailForm.cnTitle,
            cnContent: this.MailForm.cnContent,
            enTitle: this.MailForm.enTitle,
            enContent: this.MailForm.enContent,
            frenchTitle: this.MailForm.frenchTitle,
            frenchContent: this.MailForm.frenchContent,
            germanTitle: this.MailForm.germanTitle,
            germanContent: this.MailForm.germanContent,
            russianTitle: this.MailForm.russianTitle,
            russianContent: this.MailForm.russianContent,
            userMinAfk: this.MailForm.userMinAfk,
            userMinLevel: this.MailForm.userMinLevel,
            specifiedServerIds: specifiedServerIds,
          };
          this.dialog.show = false;
          this.$emit("update", obj);
     }
    },
  },
};
</script>
<style scoped>
.upload {
  width: 20px;
  height: 20px;
  margin-top: 10px;
}
.btn {
  padding: 10px;
  margin-left: 45%;
}
</style>